<?php 
class Default_Model_Customer extends Henry_Db_Table
{
	static private $TBNAME = "customer";
	protected $_name = 'customer'; 
  	protected $_primary = 'customer_id';

  	const CLOCK = 0;
  	const NORMAL = 1;
	public function init()
	{
		$this->_name = parent::PREFIX.$this->_name;
	}
	public function listCustomer($data = null)
	{
		$select = $this->select($this->_name);		
		return $select;
	}
	public function changeStatus($id)
	{
		//Get current Status;
		$select = $this->_db->select();
		$select->from(array('c' => $this->_name),array('customer_id','customer_status'))
			   ->where('c.customer_id = ?',$id);
		$result =  $this->_db->fetchRow($select);
		$data=array();
		$data['customer_status'] = $result['customer_status']==Default_Model_Customer::NORMAL?Default_Model_Customer::CLOCK:Default_Model_Customer::NORMAL;
		$this->updateCustomer($id,$data);
	}
	public function getIdAndName($ngay = null)
	{
		$select = $this->_db->select()
					   ->from(array('c'=>$this->_name),'customer_id')						  
					   ->columns("customer_fname")
					   ->columns("customer_lname")
					   ->where("customer_status = ?",self::NORMAL);
		$data = $this->_db->query($select)->fetchAll(Zend_Db::FETCH_ASSOC);
		$cnOb = new Default_Model_Congno();
		if ($ngay == null) return $data;
		foreach ($data as $key=>$item)
		{
			$tmpSoTien = $cnOb->laySoTien($item['customer_id'],$ngay);
			$data[$key]['sotien_chieu'] = $tmpSoTien['sotien_chieu'];
			$data[$key]['sotien_hn'] = $tmpSoTien['sotien_hn'];
		}	
		return $data;
	} 
	public function addCustomer($data)
	{	
		return $this->insert($data);
	}
	public function getCustomer($id)
	{
		$result = $this->find($id)->toArray();
		return $result[0];
	}
	public function delCustomer($id)
	{
		$db = $this->_db;
		$db->beginTransaction();
		try{
			//Xoa Cong No
			$cnOb = new Default_Model_Congno();
			$cnOb->xoaCongNobyCusId($id);
			//Xoa Tong Cong No
			$tongCn = new Default_Model_TongCongNo();
			$tongCn->xoaTongCongNo($id);
			//Xoa Thu Chi
			$thuChiOb = new Default_Model_ThuChi();
			$thuChiOb->xoa($id);
			$where = $this->_db->quoteInto('customer_id = ?', $id);
			$this->delete($where);
			$db->commit();
	    	return true;
		}
		catch(Exception $e)
		{
			$db->rollBack();
		    echo $e->getMessage();
		    return false;
		}
	}
	public function updateCustomer($id,$data)
	{
		$where = $this->_db->quoteInto('customer_id = ?', $id);
		return $this->update($data,$where);
	}
	
	public function getName($id)
	{
		$select = $this->_db->select()
					   ->from(array('c'=>$this->_name),'customer_id')						  
					   ->columns("customer_lname")
					   ->columns("customer_fname")
					   ->where('customer_id = ?', $id);
		$data = $this->_db->query($select)->fetchAll(Zend_Db::FETCH_ASSOC);
		return $data[0]['customer_lname']." ".$data[0]['customer_fname'];	
	}
	public static function getTBName()
	{
		return parent::PREFIX.self::$TBNAME; 
	}
}